//57步进电机引脚
#define DIR_57_1 48 //方向信号
#define DIR_57_0 49 //方向信号
#define PUL_57_1 46 //脉冲信号
#define PUL_57_0 47 //脉冲信号

#define DIR_1 32 //方向信号
#define DIR_0 33 //方向信号
#define PUL_1 30 //脉冲信号
#define PUL_0 31 //脉冲信号

//右边42步进电机引脚
#define DIR_42_right_1 52 //方向信号
#define DIR_42_right_0 53 //方向信号
#define PUL_42_right_1 50 //脉冲信号
#define PUL_42_right_0 51 //脉冲信号
//左边42步进电机引脚
#define DIR_42_left_1 24 //方向信号
#define DIR_42_left_0 25 //方向信号
#define PUL_42_left_1 22 //脉冲信号
#define PUL_42_left_0 23 //脉冲信号

//86步进电机引脚
#define DIR_86_1 42 //方向信号
#define DIR_86_0 43 //方向信号
#define PUL_86_1 40 //脉冲信号
#define PUL_86_0 41 //脉冲信号

char val;

void setup() {
  
  //引脚初始化
  pinMode(DIR_57_1,OUTPUT);
  pinMode(DIR_57_0,OUTPUT);
  pinMode(PUL_57_1,OUTPUT);
  pinMode(PUL_57_0,OUTPUT);
  pinMode(DIR_1,OUTPUT);
  pinMode(DIR_0,OUTPUT);
  pinMode(PUL_1,OUTPUT);
  pinMode(PUL_0,OUTPUT);
  pinMode(DIR_42_right_1,OUTPUT);
  pinMode(DIR_42_right_0,OUTPUT);
  pinMode(PUL_42_right_1,OUTPUT);
  pinMode(PUL_42_right_0,OUTPUT);
  pinMode(DIR_42_left_1,OUTPUT);
  pinMode(DIR_42_left_0,OUTPUT);
  pinMode(PUL_42_left_1,OUTPUT);
  pinMode(PUL_42_left_0,OUTPUT);
  pinMode(DIR_86_1,OUTPUT);
  pinMode(DIR_86_0,OUTPUT);
  pinMode(PUL_86_1,OUTPUT);
  pinMode(PUL_86_0,OUTPUT);

  //引脚置零，方便后面改变方向
  digitalWrite(DIR_57_1,LOW);
  digitalWrite(DIR_57_0,LOW);
  digitalWrite(PUL_57_1,LOW);
  digitalWrite(PUL_57_0,LOW);
  digitalWrite(DIR_1,LOW);
  digitalWrite(DIR_0,LOW);
  digitalWrite(PUL_1,LOW);
  digitalWrite(PUL_0,LOW);
  digitalWrite(DIR_86_1,LOW);
  digitalWrite(DIR_86_0,LOW);
  digitalWrite(PUL_86_1,LOW);
  digitalWrite(PUL_86_0,LOW);
  digitalWrite(DIR_42_right_1,LOW);
  digitalWrite(DIR_42_right_0,LOW);
  digitalWrite(PUL_42_right_1,LOW);
  digitalWrite(PUL_42_right_0,LOW);
  digitalWrite(DIR_42_left_1,LOW);
  digitalWrite(DIR_42_left_0,LOW);
  digitalWrite(PUL_42_left_1,LOW);
  digitalWrite(PUL_42_left_0,LOW);
  
  Serial.begin(9600); 
  Serial1.begin(9600); 
}
 
void loop() {
  if(Serial1.available()){
    val = char(Serial1.read());
    //停止
    if(val=='0'){
      Serial.print(0);
      Move_stop();
      }
    //前进
    if(val=='1'){
      Serial.print(1);
      Move_forward();
      }
    //后退
    if(val=='2'){
      Serial.print(2);
      Move_back();
      }
    //左转
    if(val=='3'){
      Serial.print(3);
      Move_left();
      }
    //右转
    if(val=='4'){
      Serial.print(4);
      Move_right();
      }
    //勾韭菜
    if(val=='5'){
      Serial.print(5);
      Tick();
      }
    if(val=='6'){
      Serial.print(6);
      Tick_0();
      }
    if(val=='7'){
      Serial.print(7);
      nanbeng();
      }
    if(val=='8'){
      Serial.print(8);
      all();
      }
  }
}

//前进
void Move_forward(){
  digitalWrite(DIR_57_1,HIGH);
  digitalWrite(DIR_42_right_0,HIGH);
  digitalWrite(DIR_42_left_1,HIGH);
  while(1){
    digitalWrite(PUL_57_1,HIGH);
    digitalWrite(PUL_42_right_1,HIGH);
    digitalWrite(PUL_42_left_1,HIGH);
    delayMicroseconds(50);//这个函数单位为微秒
    digitalWrite(PUL_57_1,LOW);
    digitalWrite(PUL_42_right_1,LOW);
    digitalWrite(PUL_42_left_1,LOW);
    delayMicroseconds(1000);
  
    if(Serial1.available()) {
      digitalWrite(DIR_57_1,LOW);
      digitalWrite(DIR_42_right_0,LOW);
      digitalWrite(DIR_42_left_1,LOW);
      break;
      }
    }
    
}

//后退
void Move_back(){
  digitalWrite(DIR_57_0,HIGH);
  digitalWrite(DIR_42_right_1,HIGH);
  digitalWrite(DIR_42_left_0,HIGH);
  while(1){
    digitalWrite(PUL_57_1,HIGH);
    digitalWrite(PUL_42_right_1,HIGH);
    digitalWrite(PUL_42_left_1,HIGH);
    delayMicroseconds(50);//这个函数单位为微秒
    digitalWrite(PUL_57_1,LOW);
    digitalWrite(PUL_42_right_1,LOW);
    digitalWrite(PUL_42_left_1,LOW);
    delayMicroseconds(1000);

    if(Serial1.available()) {
      digitalWrite(DIR_57_0,LOW);
      digitalWrite(DIR_42_right_1,LOW);
      digitalWrite(DIR_42_left_0,LOW);
      break;
      }
    }
  }

//左转
void Move_left(){
  digitalWrite(DIR_57_1,HIGH);
  digitalWrite(DIR_42_right_0,HIGH);
  digitalWrite(DIR_42_left_0,HIGH);
  while(1){
    digitalWrite(PUL_57_1,HIGH);
    digitalWrite(PUL_42_right_1,HIGH);
    digitalWrite(PUL_42_left_1,HIGH);
    delayMicroseconds(50);//这个函数单位为微秒
    digitalWrite(PUL_57_1,LOW);
    digitalWrite(PUL_42_right_1,LOW);
    digitalWrite(PUL_42_left_1,LOW);
    delayMicroseconds(1000 );

    if(Serial1.available()) {
      digitalWrite(DIR_57_0,LOW);
      digitalWrite(DIR_42_right_1,LOW);
      digitalWrite(DIR_42_left_0,LOW);
      break;
      }
    }
  }

//右转
void Move_right(){
  digitalWrite(DIR_57_1,HIGH);
  digitalWrite(DIR_42_right_1,HIGH);
  digitalWrite(DIR_42_left_1,HIGH);
  while(1){
    digitalWrite(PUL_57_1,HIGH);
    digitalWrite(PUL_42_right_1,HIGH);
    digitalWrite(PUL_42_left_1,HIGH);
    delayMicroseconds(50);//这个函数单位为微秒
    digitalWrite(PUL_57_1,LOW);
    digitalWrite(PUL_42_right_1,LOW);
    digitalWrite(PUL_42_left_1,LOW);
    delayMicroseconds(100);

    if(Serial1.available()) {
      digitalWrite(DIR_57_1,LOW);
      digitalWrite(DIR_42_right_1,LOW);
      digitalWrite(DIR_42_left_1,LOW);
      break;
      }
    }
  }

//右转
//勾韭菜模块
void Tick(){
  digitalWrite(DIR_1,HIGH);
  maichong(PUL_1,9600,100);
  digitalWrite(DIR_1,LOW);
  }

void Tick_0(){
  digitalWrite(DIR_0,HIGH);
  maichong(PUL_1,9600,100);
  digitalWrite(DIR_0,LOW);
  }

void nanbeng(){
  digitalWrite(DIR_86_1,LOW);
  while(1){
    digitalWrite(PUL_86_1,HIGH);
    delayMicroseconds(50);//这个函数单位为微秒
    digitalWrite(PUL_86_1,LOW);
    delayMicroseconds(100);

    if(Serial1.available()) {
    digitalWrite(DIR_86_1,LOW);
    digitalWrite(PUL_86_1,LOW);
    break;
    }
    
    }
  }
void Move_stop(){
  digitalWrite(DIR_57_1,LOW);
  digitalWrite(DIR_57_0,LOW);
  digitalWrite(PUL_57_1,LOW);
  digitalWrite(PUL_57_0,LOW);
  digitalWrite(DIR_42_right_1,LOW);
  digitalWrite(DIR_42_right_0,LOW);
  digitalWrite(PUL_42_right_1,LOW);
  digitalWrite(PUL_42_right_0,LOW);
  digitalWrite(DIR_42_left_1,LOW);
  digitalWrite(DIR_42_left_0,LOW);
  digitalWrite(PUL_42_left_1,LOW);
  digitalWrite(PUL_42_left_0,LOW);
  }

void all(){
  Move_forward();
  nanbeng();
  Tick();
  Tick_0();
  }
//控制电机运动
void maichong(int Pin,int times,int speed)//times是脉冲的数量，speed是脉冲间隔，对应着电机的速度
{
  for(int x=0; x<times; x++){
    digitalWrite(Pin,HIGH);
    delayMicroseconds(50);//这个函数单位为微秒
    digitalWrite(Pin,LOW);
    delayMicroseconds(50);
    delayMicroseconds(speed);
  }
}
